home *** CD-ROM | disk | FTP | other *** search
/ InfoMagic Internet Tools 1995 April / Internet Tools.iso / mail / listserv / utils / accept_mail.pl.Z / accept_mail.pl
Encoding:
Perl Script  |  1992-05-25  |  2.9 KB  |  117 lines

  1. #!/usr/local/bin/perl
  2. #
  3. # accept_l_mail - accept Listserv mail
  4. #
  5. # this script is invoked by Sendmail via the setuid wrapper to 
  6. # deliver mail to a list.  It does some checks to make sure
  7. # this is not a subscribe or unsubscribe command, then invokes
  8. # catmail to append to appropriate mail file.  (We use catmail
  9. # because it knows how to lock the mail file.)
  10. #
  11. # usage: accept_l_mail list_directory
  12. #
  13. # Author: "Michael H. Morse" <mmorse@z.nsf.gov>
  14.  
  15. $ENV{'PATH'} = '/bin:/usr/bin';
  16. $ENV{'IFS'} = '' if $ENV{'IFS'} ne '';  # from Camel book
  17.  
  18. umask(022);
  19. $tmpdir = "/tmp";
  20. $tmpfile = "$tmpdir/accept$$";
  21. $list = $ARGV[0];
  22. if (!$list){
  23.    die("Invokation error.\n");
  24. }
  25. $catmail = "/usr/server/catmail";
  26.  
  27. # copy file to tmp file, looking for suspicious lines, and picking
  28. # up the "from:" field, just in case.
  29.  
  30. $in_msg = 0;
  31. $from = "";
  32. $got_command = 0;
  33. $got_other_stuff = 0;
  34. open(TMP,">$tmpfile") || die("Can't open $tmpfile\n");
  35. while (<STDIN>){
  36.    chop;
  37.    if ($_ =~ /^\s*$/){
  38.       $in_msg++;
  39.    }
  40.    elsif (! $in_msg){
  41.       # look for from:
  42.       if ($_ =~ /^From:/){
  43.          $from = $_;
  44.          $from =~ s/From:\s*//;
  45.       }
  46.    }
  47.    else {              # in the message
  48.       if ($_ =~ /^\s*subscribe(\s|$)/i ||
  49.           $_ =~ /^\s*unsubscribe(\s|$)/i ){
  50.           $got_command++;
  51.       }
  52.       else {
  53.           $got_other_stuff++;
  54.       }
  55.    }
  56.    print(TMP "$_\n");
  57. }
  58. close(TMP);
  59. # analyze what we got
  60. if ($from && $got_command && ($got_other_stuff < 5)){
  61.    &kill_message;
  62. }
  63. else {
  64.    &post_message;
  65. }
  66. unlink($tmpfile);
  67. exit 0;
  68.  
  69. sub post_message{
  70.    open(TMP,"$tmpfile") || die("Can't open $tmpfile\n");
  71.    # need to untaint $list:
  72.    $list =~ tr/a-zA-Z-//cd;  # get rid of nasty shell meta characters
  73.    $list =~ /.*/;
  74.    $utlist = $&;
  75.    open(OUT,"| $catmail -L $utlist -f") || die ("Can't run $catmail:$!\n");
  76.    while (<TMP>){
  77.       print(OUT $_);
  78.    }
  79.    close(OUT);
  80. }
  81.  
  82. sub kill_message {
  83.    open(MAIL, "|/usr/lib/sendmail -t") 
  84.                        || die ("Can't run sendmail: $!\n");
  85.    
  86.    print MAIL <<EOF;
  87. From: NSF Listserv <stisop@nsf.gov>
  88. To: $from
  89. bcc: mmorse@nsf.gov
  90. Subject: Suspicious posting to list
  91.  
  92. Hello,
  93.    You just sent a message to "$list@nsf.gov".  Since your message
  94. included a "subscribe" or "unsubscribe" command, we've taken a chance
  95. and assumed that you meant to send it to "listserv@nsf.gov" (Internet)
  96. or "listserv@NSF" (BITNET).  All messages to subscribe or unsubscribe
  97. should go to one of those addresses.
  98.  
  99.    If we were correct, please re-send your message to Listserv instead
  100. of the list itself.  If we erred, and you really meant for your message
  101. to be re-distributed to all the list subscribers, please re-format 
  102. your message so that the Listserv command is not the first word on 
  103. the line, and re-send it (and accept our apologies).
  104.    
  105.    This message is automatically generated.
  106.  
  107.    Your message to us is included below.
  108.  
  109. EOF
  110.    open(TMP,"$tmpfile") || die("Can't open $tmpfile\n");
  111.    while (<TMP>){
  112.       print(MAIL "> $_");
  113.    }
  114.  
  115.    close MAIL;
  116. }
  117.